﻿using UnityEngine;
using System.Collections;

public class sc : MonoBehaviour
{
    public float MoveSpeed = 5.0F;
    public float JumpHeight = 5.0F;
    private float JumpForce = 0F;
    private float Ltime,Ctime;
    private string AttackName;
    private Animator animator;
    public int MAXHealth;
    public int Health;
    public int Magic;
    private int lockx=0;

    private bool busy;
    private bool selfbusy = false;

    private bool right = false;
    private bool free=true;
    private int attacking = 1;
    public GameObject SwordWindPrefab;
    private bool swordwinding = false;
    // Use this for initialization
    void Start()
    {
        Ltime = Time.time;
        Health = MAXHealth;
        animator = GetComponent<Animator>();
        gameObject.transform.FindChild("ga_hand").gameObject.GetComponent<BoxCollider2D>().enabled = false;
    }
    public void set_attacking()
    {
        attacking = 0;
    }
    public int get_attacking()
    {
        return attacking;
    }
    public void TakeDamage(int damage) {
        if (busy) return;
            Health -= damage;
      //  busy = true;
        if (Health <= 0)
        {
            Die();
        }
        else {
            animator.Play("ga_attacked");
        }
    }
    void Die() {
        busy = true;
        animator.Play("ga_die");
    }
    public string GetAttackName() {
        return AttackName;
    }
    void Update()
    {
       
        if (busy||selfbusy) return;
        //busy状态不接受用户输入

        if (Input.GetKey(KeyCode.LeftArrow))
        {
            if (!right) {
                transform.Rotate(new Vector3(0, 180, 0));
                right = true;
                return;
            }
            Walk();

        }
        if (Input.GetKey(KeyCode.RightArrow))
        {
            if (right)
            {
                transform.Rotate(new Vector3(0, 180, 0));
                right = false;
            }
            Walk();

        }
        if (Input.GetKey(KeyCode.Space))
        {
            JumpForce = JumpHeight;
        }
        if (Input.GetKey(KeyCode.X))
        {
            Attack();
        }
        if (Input.GetKey(KeyCode.C)) {
            SwordWind();
        }
        if (Input.GetKey(KeyCode.D))
        {
           BackJump();
        }
        if (Input.GetKey(KeyCode.F)) {
            AngerAttack();
        }

    }
    void AngerAttack() {
        AttackName = "AngerAttack" + Time.time;
        animator.Play("ga_anger_pre");
    }
    void BackJump() {
        animator.Play("ga_backjump");
    }

    void SwordWind() {
        if (Magic < 20) return;
        Magic -= 20;
        animator.Play("ga_swordwind");
        //GameObject newObject = Instantiate(SwordWindPrefab, this.transform.position+new Vector3(0,3,0), this.transform.rotation) as GameObject;
        //在lateupdate中，完成动画释放冲击波
    }


    void Walk() {
        transform.Translate(Vector3.right * Time.deltaTime * MoveSpeed);
        if (free) animator.Play("ga_walk");

    }
    void Attack() {
        gameObject.transform.FindChild("ga_hand").gameObject.GetComponent<BoxCollider2D>().enabled = true;
        animator.Play("ga_attack");
        AttackName = "Attack" + Time.time;
        transform.FindChild("ga_hand").gameObject.GetComponent<attack>().SetDamageType(0);
        transform.FindChild("ga_hand").gameObject.GetComponent<attack>().SetString(AttackName);

        //  transform.FindChild("ga_hand").SendMessage("SetDamageType",0);
    }

    void LateUpdate()
    {
        Ctime = Time.time;
        if (Ctime-Ltime >= 0.1) {
            if (Magic <= 100) {
                Magic++;
                Ltime = Ctime;
            }
            
        }

        //根据正在执行的动画进行判断，好处是所见即所得，坏处就是繁琐，需要简化逻辑。
        //目前对人物的状态判定存在稳定


        if (animator.GetCurrentAnimatorStateInfo(0).IsName("ga_anger_pre"))
        {
            selfbusy = true;
        }
        if (animator.GetCurrentAnimatorStateInfo(0).IsName("ga_anger_do"))
        {
            selfbusy = true;
            gameObject.transform.FindChild("ga_hand").gameObject.GetComponent<BoxCollider2D>().enabled = true;
            transform.FindChild("ga_hand").gameObject.GetComponent<attack>().SetDamageType(1);
            transform.FindChild("ga_hand").gameObject.GetComponent<attack>().SetString(AttackName);

        }


        if (animator.GetCurrentAnimatorStateInfo(0).IsName("ga_attack"))
        {
            selfbusy = true;
        }
        if (animator.GetCurrentAnimatorStateInfo(0).IsName("ga_idle"))
        {
            selfbusy = false;
            busy = false;
        }
        if (animator.GetCurrentAnimatorStateInfo(0).IsName("ga_jump"))
        {
            selfbusy = false;
            busy = false;
        }
        if (animator.GetCurrentAnimatorStateInfo(0).IsName("ga_attacked"))
        {
            selfbusy = false;
            busy = true;
        }
        if (animator.GetCurrentAnimatorStateInfo(0).IsName("ga_die"))
        {
            selfbusy = false;
            busy = true;
        }
        if ((animator.GetCurrentAnimatorStateInfo(0).normalizedTime > 1) && (animator.GetCurrentAnimatorStateInfo(0).IsName("ga_die")))
        {
            Destroy(this.gameObject);
        }
        if (animator.GetCurrentAnimatorStateInfo(0).IsName("ga_walk"))
        {
            selfbusy = false;
            busy = false;
        }
        if (animator.GetCurrentAnimatorStateInfo(0).IsName("ga_swordwind"))
        {
            selfbusy = true;
            busy = false;
        }
        if ((animator.GetCurrentAnimatorStateInfo(0).normalizedTime > 1) && (animator.GetCurrentAnimatorStateInfo(0).IsName("ga_swordwind")))
        {
            if (!swordwinding)
            {
                GameObject newObject = Instantiate(SwordWindPrefab, this.transform.position + new Vector3(0, 3, 0), this.transform.rotation) as GameObject;
                swordwinding = true;
            }
        }

        if ((animator.GetCurrentAnimatorStateInfo(0).normalizedTime < 1) && (animator.GetCurrentAnimatorStateInfo(0).IsName("ga_backjump")))
        {

            if (animator.GetCurrentAnimatorStateInfo(0).normalizedTime < 0.5)
            {
                transform.Translate(Vector3.up * Time.deltaTime * MoveSpeed); ;
                selfbusy = true;
            }
            else
                selfbusy = false;
            transform.Translate(Vector3.left * Time.deltaTime * MoveSpeed);
        }

        //if (!animator.GetCurrentAnimatorStateInfo(0).IsName("ga_attack")) {
        //    attacking = 1;
        //    gameObject.transform.FindChild("ga_hand").gameObject.GetComponent<BoxCollider2D>().enabled = false;
        //}

          if (!animator.GetCurrentAnimatorStateInfo(0).IsName("ga_swordwind")){
            swordwinding = false;
        }

        if (!selfbusy)
        {
            attacking = 1;
            gameObject.transform.FindChild("ga_hand").gameObject.GetComponent<BoxCollider2D>().enabled = false;

        }



        transform.Translate(Vector3.up * Time.deltaTime * JumpForce);
        JumpForce -= 0.1F;
        if (JumpForce < 0)
            JumpForce = 0;
    }
}
